Reorganizing
Added test cases
Non-ref elements works as well now