Splitted code into classes. Algorithm for base=3 added.