working fine, needs refactor