Cleaned up a bit

got rid of some redundant code and rearranged/add/fixed some of the
error handling