Modifications to fasten the code and make more flexible