Rearanged the code and added throws to some doc blocks