Solidify code and better error handling