refactor of fragment code and some cleanup and fixes