added test cases and swaped some for-loops for lapply's