Lagrangian implemented but not working properly