Reformatted code, cleaned up obvious coding errors