Code clean up and miscellaneous fixes