Added better error messages, refactored a bit.