error handling and formatting improvements
more major refactoring