Clean up code, reorganize, squash bugs