replaced code and fixed bug