Added all missing functionalities.
Note: the code compiles, but has not been tested. There *are* bugs.
However, this is approximately how I wanted to have it to be in a good shape.