Simplified code, still need to test and adapt