some fixes left and right, and added more flexibility (less hard-coded values)