Fixed bugs and added error messages
Merged code