Closes #1723 SQL by Staleness89, corrections by Killyana Script by Keader, corrections by sirikfoll and me