CURATNTOOL-1064 - speeding up details page

Moved sub query into upper loop/main proc
Cleaned up lots of other little bits.
ignoring Intellij files