Fix namespace bug & other fixes

* Fix namespace bug
* Use mapcat & butlast instead of user defined fns
* Minor style fixes