Code cleanup

Replace 200 lines of bad code with 20 lines of better code :D