Fixed some errors and moved code around