(1) Minor refactoring
(2) More error checking