Clean duplication of code and fixed bug