updated code to fix bugs and use better practices