cleaned up a bit, handled errors better