Exception handling added, several errors fixed