added version with classes, tweaked existing code