Small bugfix and code cleanup

This is the final commit of the branch as it seems like all kinks have
been ironed out. This will be merged after testing is complete