Added more error checks, performance improvements, try catch blocks and other improvements