Clean-up code some more, better handle errors