refactored and cleaned up fx code