updated code into somewhat objoriented