Lots of additions and code cleanup