Split some code into a new file, fixed some bugs, etc