Refactoring and error handling

fixed some errors in main.js and added some  additional checks