Cleaned and restructured parts of code