Added more error checking & reorganized methods.