The solution for this problem is a mess and not elegant at all, too many conditional branches to handle different cases, a more clean solution is needed.