Better error handling and removal of old code