tests and better error handling
reorganize and add tests