Improved error handling and tested some functions

There are problems with the heavier function, where the value is
returned before all the data is retrieved