general improvements all over the code and some modifications in logic