Fixed error handling. General cleanup