Refactored fragment function
Added tests