Fix small bugs and make small adjusts to the code