Fixed bugs, better error handling