Cleaned and fixed code from bad branch merger