Moved things around, added better logic to the internal calculations