Miscellaneous improvements and cleanup

Just did some housekeeping and minor refactors. You can see the full details in the commit.