Refactored some things, implemented some missing steps, etc.

Please take a look at comments below for understanding the main changes