Add error handling. Refactor to require more use of base datatypes