Refactored top level code now working.