Add function to calculate the absolute offset of an element

Also, reorganize the code