Added error detection thingy, code optimization.