cleaned up and merged code