additional code refactor and bug fixes; still not working