minor code cleanup
better error handling

added console.log() msg for error checking