working version but needs some cleanup