Added proper exception handling. Work in progress