Reworking code. Pushed for ref.