Adjusted based on the latest if
Merged changes made by Jakub
Merged change