Added functionality to read attributes, and rearranged some code