Heavily refactored. Only refresh issue remains