Fixed minior bugs, optimized error handling