Fixed bugs, added more error messages.