still need to clean up the code but this is working version