Cleaning code a bit
Rewrote to support new data structure