Add comments and improve code