cleaned up the source a bit to make it more error-hander friendly