Adds more debugging code and organizes some functions.