Some clean up on the code (and some fixes too)