refactored code by splitting up certain functions. Included performance enhancement for writing attributes