Implemented missing functionalities

Implemented missing functionalities plus slightly refactored exsisting
code.