Adds almost complete test coverage + minor cleanup