Cleaned up code, added error messages