make the code a bit more flexible

Adjusted for different data structures